package com.zoweunion.mechanic.util.task;

import cn.jiguang.common.utils.StringUtils;
import com.aliyun.rds20140815.models.ModifySecurityIpsRequest;
import com.zoweunion.mechanic.entity.Sample;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Deprecated
public class SampleTask {

    @Scheduled(cron = "0 0 7 * * ?")
    public void sample() throws Exception {
        com.aliyun.rds20140815.Client client =
                Sample.createClient("LTAI5tFcgmibQug2CmZLSCwH", "I4dI5YpvQREPExOuCNYdxiJWFYhsRC");
        String ip = null;
        BufferedReader br = null;
        try {
            URL url = new URL("https://v6r.ipip.net/?format=callback");
            br = new BufferedReader(new InputStreamReader(url.openStream()));
            String s = "";
            StringBuffer sb = new StringBuffer("");
            String webContent = "";
            while ((s = br.readLine()) != null) {
                sb.append(s + "\r\n");
            }
            webContent = sb.toString();
            int start = webContent.indexOf("(") + 2;
            int end = webContent.indexOf(")") - 1;
            webContent = webContent.substring(start, end);
            ip = webContent;
        } finally {
            if (br != null)
                br.close();
        }
        if (StringUtils.isEmpty(ip)) {
            throw new RuntimeException();
        }

        ModifySecurityIpsRequest modifySecurityIpsRequest = new ModifySecurityIpsRequest()
                // 获取公司本地ip
                .setSecurityIps(ip)
                // 固定死
                .setDBInstanceIPArrayName("intranet")
                // 固定死
                .setDBInstanceId("rm-uf656dbk5z82x2s9g");
        // 复制代码运行请自行打印 API 的返回值
        client.modifySecurityIps(modifySecurityIpsRequest);
    }


}
